package com.sh.data.engine.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/17 17:32
 */
public class DataUtil {

    public static Timestamp getTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    private static final ThreadLocal<SimpleDateFormat> threadLocal =
        ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    private static final ThreadLocal<SimpleDateFormat> threadLocalyyyyMMddHHmmss =
        ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));

    private static final ThreadLocal<SimpleDateFormat> threadLocalyyyyMMdd =
        ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMdd"));

    private static final ThreadLocal<SimpleDateFormat> threadLocalyyyy_MM_dd_HH_mm =
        ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm"));

    private static final ThreadLocal<SimpleDateFormat> threadLocalyyyy_mm_dd =
        ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

    private static final ThreadLocal<SimpleDateFormat> threadLocalyyyy_mm =
        ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM"));

    private static final ThreadLocal<SimpleDateFormat> threadLocalHH_mm =
        ThreadLocal.withInitial(() -> new SimpleDateFormat("HH:mm"));

    private static final ThreadLocal<SimpleDateFormat> threadLocalHH_mm_ss =
        ThreadLocal.withInitial(() -> new SimpleDateFormat("HH:mm:ss"));

    public static String format(Date date) {
        return threadLocal.get().format(date);
    }

    public static String formatyyyyMMddHHmmss(Date date) {
        return threadLocalyyyyMMddHHmmss.get().format(date);
    }

    public static String formatyyyyMMdd(Date date) {
        return threadLocalyyyyMMdd.get().format(date);
    }

    public static String formatyyyy_MM_dd_HH_mm(Date date) {
        return threadLocalyyyy_MM_dd_HH_mm.get().format(date);
    }

    public static String formatyyyy_MM_dd(Date date) {
        return threadLocalyyyy_mm_dd.get().format(date);
    }

    public static String formatyyyy_MM(Date date) {
        return threadLocalyyyy_mm.get().format(date);
    }

    public static String formatHH_mm(Date date) {
        return threadLocalHH_mm.get().format(date);
    }

    public static String formatHH_mm_ss(Date date) {
        return threadLocalHH_mm_ss.get().format(date);
    }

    public static Timestamp parse(String str) {
        try {
            return new Timestamp(threadLocal.get().parse(str).getTime());
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static Timestamp parseyyyy_MM_dd_HH_mm(String str) {
        try {
            return new Timestamp(threadLocalyyyy_MM_dd_HH_mm.get().parse(str).getTime());
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static Timestamp parseyyyy_MM_dd(String str) {
        try {
            return new Timestamp(threadLocalyyyy_mm_dd.get().parse(str).getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Timestamp getNowDate() {
        return parseyyyy_MM_dd(formatyyyy_MM_dd(getTimestamp()));
    }

    // "yyyyMMddHHmmss"转"yyyy-MM-dd HH:mm:ss"
    public static String parseyyyy_MM_dd_HH_mm_ss(String strDate) {
        Date date = new Date();
        try {
            date = threadLocalyyyyMMddHHmmss.get().parse(strDate);//先按照原格式转换为时间
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return threadLocal.get().format(date);//再将时间转换为对应格式字符串
    }
}
